﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Utility;
using System.Net;
using System.IO;
using Utility.Log;

namespace MsgCenter.Service.Helper
{
    public class AppConfigHelper
    {
        public static string strMessageCenter_RoleID;

        public static int strAlertTimer;

        public static string strSendMobileURL;

        public static string strSMS_SIGN;

        public static string strAdmin_UserName;

        public static int strAlertTimer_Sync;

        public static int strIsSync;

        public static string Aliyun_SIGN;

        public static string Aliyun_AccessKeyID;

        public static string Aliyun_AccessKeySecret;

        public static string Aliyun_TemplateCode;

        public static string Aliyun_RegionID;

        public static string Aliyun_Product;

        public static string Aliyun_Domain;

        public static int strIsDataSync;
        public static int strIsSmsSync;
        public static int strIsWeChatSync;

        public static string WxAppID;
        public static string WxAppSecret;
        public static string WxTempleteID_Consult;
        public static string WxTempleteID_Score;
        public static string WxHome;
        public static int strAlertTimer_WeChat;
        public static void SetConfig()
        {
            WxHome = ConfigurationManager.AppSettings["WxHome"].ToSafeString();
            WxTempleteID_Consult = ConfigurationManager.AppSettings["WxTempleteID_Consult"].ToSafeString();
            WxTempleteID_Score = ConfigurationManager.AppSettings["WxTempleteID_Score"].ToSafeString();
            WxAppID = ConfigurationManager.AppSettings["WxAppID"].ToSafeString();
            WxAppSecret = ConfigurationManager.AppSettings["WxAppSecret"].ToSafeString();
            strAlertTimer_WeChat = ConfigurationManager.AppSettings["AlertTimer_WeChat"].ToSafeInt();


            strIsWeChatSync = ConfigurationManager.AppSettings["IsWeChatSync"].ToSafeInt();
            strIsDataSync = ConfigurationManager.AppSettings["IsDataSync"].ToSafeInt();
            strIsSmsSync = ConfigurationManager.AppSettings["IsSmsSync"].ToSafeInt();

            strAdmin_UserName = ConfigurationManager.AppSettings["Admin_UserName"].ToSafeString();
            strMessageCenter_RoleID = ConfigurationManager.AppSettings["MessageCenter_RoleID"].ToSafeString();
            strSMS_SIGN = ConfigurationManager.AppSettings["SMS_SIGN"].ToSafeString();
            strAlertTimer = ConfigurationManager.AppSettings["AlertTimer"].ToSafeInt();
            strAlertTimer_Sync = ConfigurationManager.AppSettings["AlertTimer_Sync"].ToSafeInt();
            strIsSync = ConfigurationManager.AppSettings["IsSync"].ToSafeInt();
            strSendMobileURL = ConfigurationManager.AppSettings["mobileWebAddress"] + "msg/HttpSendSM?account=" + ConfigurationManager.AppSettings["SMS_USERNAME"] + "&pswd=" + ConfigurationManager.AppSettings["SMS_PASS"] + "&mobile={0}&msg={1}&needstatus=true";
            Aliyun_SIGN = ConfigurationManager.AppSettings["Aliyun_SIGN"].ToSafeString();
            Aliyun_AccessKeyID = ConfigurationManager.AppSettings["Aliyun_AccessKeyID"].ToSafeString();
            Aliyun_AccessKeySecret = ConfigurationManager.AppSettings["Aliyun_AccessKeySecret"].ToSafeString();
            Aliyun_TemplateCode = ConfigurationManager.AppSettings["Aliyun_TemplateCode"].ToSafeString();
            Aliyun_RegionID = ConfigurationManager.AppSettings["Aliyun_RegionID"].ToSafeString();
            Aliyun_Product = ConfigurationManager.AppSettings["Aliyun_Product"].ToSafeString();
            Aliyun_Domain = ConfigurationManager.AppSettings["Aliyun_Domain"].ToSafeString();
        }

        public static string HttpRequestUrlData(string url)
        {
            return HttpRequestUrlData(url, "电信").Replace("\n", ",");
        }

        public static string HttpRequestUrlData(string url, string strTypeName)
        {
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            StreamReader streamReader = null;
            string result = string.Empty;
            try
            {
                httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.Method = "GET";
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                streamReader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
                result = streamReader.ReadToEnd();
                responseStream.Dispose();
                LogHelper.WriteLog("请求" + strTypeName + "URL地址为：" + url + "      成功");
                return result;
            }
            catch (Exception se)
            {
                LogHelper.WriteError("请求" + strTypeName + "URL地址为：" + url + "      失败", se);
                return result;
            }
            finally
            {
                streamReader.Close();
                httpWebResponse.Close();
                httpWebRequest.Abort();
            }
        }
    }
}
